package com.onlinevideo.servlet;


import cn.hutool.json.JSONUtil;
import com.onlinevideo.data.ResultBean;

import com.onlinevideo.entity.RoleAuthority;
import com.onlinevideo.entity.RoleAuthorityDTO;
import com.onlinevideo.service.RoleAuthorityService;
import com.onlinevideo.service.impl.RoleAuthorityServiceImpl;
import com.onlinevideo.util.MyBeanUtils;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

public class RoleAuthorityServlet extends BaseServlet{
    private PrintWriter writer = null;
    private RoleAuthorityService roleAuthorityService = new RoleAuthorityServiceImpl();
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer = resp.getWriter();
        RoleAuthority roleAuthority = MyBeanUtils.set(RoleAuthority.class, req.getParameterMap());
        int result = roleAuthorityService.insert(roleAuthority);
        if(result>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("权限添加成功，请刷新数据")));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError("权限添加失败")));
        }
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer = resp.getWriter();
        RoleAuthority roleAuthority = MyBeanUtils.set(RoleAuthority.class, req.getParameterMap());
        int result = roleAuthorityService.delete(roleAuthority.getId());
        if(result>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("权限删除成功")));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError("权限删除失败，权限不存在")));
        }
    }

    protected void queryByRoleId(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer = resp.getWriter();

        String roleIdStr = req.getParameter("roleId");
        if(roleIdStr == null ||roleIdStr.length()<=0 ){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError("权限查找失败，未选择查找的角色")));
            return;
        }
        List<RoleAuthorityDTO> roleAuthorityDTO = roleAuthorityService.queryByRoleId(Long.valueOf(roleIdStr));
        if(roleAuthorityDTO.size()>0){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("权限查找成功",roleAuthorityDTO)));
        }else {
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError("权限查找失败，该角色无对应的权限")));
        }
    }

    protected void queryAllByStartIndexAndPageSize(HttpServletRequest req, HttpServletResponse resp) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        writer = resp.getWriter();

//        List<RoleAuthority> queryAllByStartIndexAndPageSize(Integer startIndex,Integer pageSize){
//            Integer paceNo = Integer.valueOf(req.getParameter("pageNo"));
//        }
    }
}
